package neuronvisualizer;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

public class TimelineButtonListener implements ActionListener{
	private OptionBar parent;
	public TimelineButtonListener(OptionBar p){
		parent = p;
	}
	public void actionPerformed(ActionEvent e){
		if(parent.getDriver().getTimelineGUI() != null){
			parent.getDriver().getTimelineGUI().dispose();
		}

		JFileChooser timelineChooser = new JFileChooser();
		timelineChooser.setFileFilter(new FileFilter(){

			@Override
			public boolean accept(File f) {
		        if (f.isDirectory()) {
		            return true;
		        }
		        String filename = f.getName();
		        
		        if (filename != null) {
		            if (filename.endsWith(".txt")) {
		                    return true;
		            } else {
		                return false;
		            }
		        }
		 
		        return false;
			}

			@Override
			public String getDescription() {
		        return "*.txt";
			}
				
		});
		timelineChooser.setAcceptAllFileFilterUsed(false);
		int returnVal = timelineChooser.showOpenDialog((Component) e.getSource());
		if(returnVal == JFileChooser.APPROVE_OPTION){
			File file = timelineChooser.getSelectedFile();
			String tempPath = file.getAbsolutePath();
			if(tempPath.endsWith(".txt")){
				parent.getDriver().loadTimeline(tempPath);
			}
		}
	}
}
